package com.mk.web.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.mk.web.mapper.BottleMapper;
import com.mk.web.model.Bottle;
import com.mk.web.service.BottleService;
import com.mk.web.util.MybatisUtil;

/**
 * Created by WangChen on 2018/11/27 15:41.
 */
public class BottleServiceImpl implements BottleService {

    @Override
    public List<Bottle> findByNameAndSize(String name, double size) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = factory.openSession();

        BottleMapper mapper = sqlSession.getMapper(BottleMapper.class);
        List<Bottle> list = mapper.selectByNameAndSize(name, size);

        sqlSession.close();
        return list;
    }

    @Override
    public int save(Bottle bottle) {

        SqlSession sqlSession = MybatisUtil.getSession();

        BottleMapper mapper = sqlSession.getMapper(BottleMapper.class);
        return mapper.insert(bottle);
    }

}
